<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function arr(nums, p) {
      let x = 0
      for (const num of nums) {
        x = (x + num) % p
      }
      if (x === 0) {
        return 0
      }
      const index = new Map()
      let y = 0; res = nums.length
      for (let i = 0; i < nums.length; i++) {
        index.set(y, i)
        y = (y + nums[i]) % p
        if (index.has((y - x + p) % p)) {
          res = Math.min(res, i - index.get((y - x + p) % p) + 1)
        }
      }
      return res === nums.length ? -1 : res
    };
    console.log(arr([3, 1, 4, 2], 6));
  </script>
</body>

</html>